package wsi.exp.exp5;

import java.io.File;
import java.util.LinkedList;
import java.util.List;

import org.rygh.core.util.UIo;

public class JudgeFile {

	private final List<Boolean> bools = new LinkedList<>();

	public JudgeFile(File file) {
		load(file);
	}

	public JudgeFile(int size) {
		for (int i = 0; i < size; i++)
			bools.add(null);
	}

	public Boolean get(int index) {
		return bools.get(index);
	}

	public int getCorrectSize() {
		int c = 0;
		for (Boolean b : bools)
			if (b != null && b)
				c++;

		return c;
	}

	public int getJudgedSize() {
		int c = 0;
		for (Boolean b : bools)
			if (b != null)
				c++;

		return c;
	}

	public double getPrecision() {
		int j = getJudgedSize();

		return j > 0 ? 100. * getCorrectSize() / j : Double.NaN;
	}

	public boolean isAllJudged() {
		return getJudgedSize() == size();
	}

	public boolean isEmpty() {
		return bools.isEmpty();
	}

	private void load(File file) {
		if (file == null || !file.exists())
			return;

		String content = UIo.readFile(file);

		if (content == null)
			return;

		String[] lines = content.split("\r\n");

		for (String l : lines) {
			if (l.trim().isEmpty())
				continue;

			if (l.equals("null"))
				bools.add(null);
			else
				bools.add(Boolean.parseBoolean(l));
		}
	}

	public void set(int index, Boolean b) {
		bools.set(index, b);
	}

	public int size() {
		return bools.size();
	}

	@Override
	public String toString() {
		StringBuilder sb = new StringBuilder();

		for (Boolean b : bools) {
			sb.append(b == null ? "null" : String.format("%b", b));
			sb.append("\r\n");
		}

		return sb.toString();
	}
}
